package com.uduemc.biso.master.backend.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.github.pagehelper.PageInfo;
import com.uduemc.biso.core.common.extities.CCustomerData;
import com.uduemc.biso.core.common.extities.CHostListCustomerData;
import com.uduemc.biso.core.common.feign.CCustomerFeign;
import com.uduemc.biso.core.extities.center.CustomerRole;
import com.uduemc.biso.core.extities.center.CustomerUser;
import com.uduemc.biso.core.extities.center.Host;
import com.uduemc.biso.core.extities.center.Site;
import com.uduemc.biso.core.extities.center.SysRole;
import com.uduemc.biso.core.feign.CustomerFeign;
import com.uduemc.biso.core.feign.SysRoleFeign;
import com.uduemc.biso.core.utils.RestResult;
import com.uduemc.biso.core.utils.ResultUtil;
import com.uduemc.biso.master.backend.request.CustomermanageAppend;
import com.uduemc.biso.master.backend.service.CustomermanageService;

@Service
public class CustomermanageServiceImpl implements CustomermanageService {

//	private final static Logger logger = LoggerFactory.getLogger(CustomermanageServiceImpl.class);

	@Autowired
	private SysRoleFeign sysRoleFeign;

	@Autowired
	private CCustomerFeign cCustomerFeign;

	@Autowired
	private CustomerFeign customerFeign;

	@Autowired
	private PasswordEncoder passwordEncoder;

	@Autowired
	private CustomermanageService customermanageServiceImpl;

	@Override
	public List<SysRole> getAllSysRole()
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		RestResult restResult = sysRoleFeign.findAll();
		@SuppressWarnings("unchecked")
		List<SysRole> data = (ArrayList<SysRole>) ResultUtil.data(restResult, new TypeReference<ArrayList<SysRole>>() {
		});
		return data;
	}

	@Override
	public CustomerUser getByUsername(String username)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		RestResult restResult = customerFeign.getUserByUsername(username);
		CustomerUser data = ResultUtil.data(restResult, CustomerUser.class);
		return data;
	}

	@Override
	public CustomerUser insertCustomerData(CustomermanageAppend customermanageAppend)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		CustomerUser customerUser = customermanageAppend.makeCustomerUser();
		Host host = customermanageAppend.makeHost();
		List<Site> sites = customermanageAppend.makeSites();
		List<CustomerRole> customerRoles = customermanageAppend
				.makeCustomerRoles(customermanageServiceImpl.getAllSysRole());

		customerUser.setAgentId(0L).setStatus((short) 1).setNumberKey(null)
				.setPasswordHash(passwordEncoder.encode(customerUser.getPasswordHash()));

		host.setRandomCode(null).setStatus((short) 1);

		RestResult restResult = cCustomerFeign.append(new CCustomerData(customerUser, customerRoles, host, sites));
		CCustomerData data = ResultUtil.data(restResult, CCustomerData.class);
		return data == null ? null : data.getCustomerUser();
	}

	@Override
	public PageInfo<CHostListCustomerData> infosGet(long agentId, String username, String numberKey, short status,
			int pageNumber, int pageSize)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		RestResult restResult = cCustomerFeign.infos(agentId, username, numberKey, status, pageNumber, pageSize);
		@SuppressWarnings("unchecked")
		PageInfo<CHostListCustomerData> data = (PageInfo<CHostListCustomerData>) ResultUtil.data(restResult,
				new TypeReference<PageInfo<CHostListCustomerData>>() {
				});
		return data;
	}

	@Override
	public CHostListCustomerData infoGet(long id, long agentId)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		RestResult restResult = cCustomerFeign.info(id, agentId);
		CHostListCustomerData data = ResultUtil.data(restResult, CHostListCustomerData.class);
		return data;
	}

	@Override
	public CustomerUser updateStatusCustomerUser(long id, short status)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		RestResult restResult = customerFeign.findUserOne(id);
		CustomerUser customerUser = ResultUtil.data(restResult, CustomerUser.class);
		if (customerUser == null) {
			return null;
		}
		if (customerUser.getStatus() == null || customerUser.getStatus().shortValue() != status) {
			customerUser.setStatus(status);
			restResult = customerFeign.updateUserById(customerUser);
			customerUser = ResultUtil.data(restResult, CustomerUser.class);
			if (customerUser == null) {
				return null;
			}
		}
		return customerUser;
	}

	@Override
	synchronized public CustomerUser updateUsernameCustomerUser(long id, String username)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		CustomerUser customerUser = getByUsername(username);
		if (customerUser != null) {
			return null;
		}
		RestResult restResult = customerFeign.findUserOne(id);
		CustomerUser customerUserFind = ResultUtil.data(restResult, CustomerUser.class);
		if (customerUserFind == null) {
			return null;
		}
		if (customerUserFind.getUsername().equals(username)) {
			return customerUserFind;
		}
		customerUserFind.setUsername(username);
		restResult = customerFeign.updateUserById(customerUserFind);
		customerUser = ResultUtil.data(restResult, CustomerUser.class);
		if (customerUser == null) {
			return null;
		}
		return customerUser;
	}

}
